package com.ahdy.controller;

import com.ahdy.entity.ElderlyFamilyRelation;
import com.ahdy.pojo.ResponseEntitys;
import com.ahdy.service.ElderlyFamilyRelationService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 老人与子女关联关系管理控制器
 */
@RestController
@RequestMapping("/api/relations")
public class ElderlyFamilyRelationController {

    @Autowired
    private ElderlyFamilyRelationService elderlyFamilyRelationService;

    /**
     * 添加关联关系
     *
     * @param relation 关联关系实体
     * @return 响应结果
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> addRelation(@Valid @RequestBody ElderlyFamilyRelation relation) {
        if (elderlyFamilyRelationService.checkRelationExists(relation.getElderlyId(), relation.getFamilyId())) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", "关联关系已存在");
            return ResponseEntity.badRequest().body(response);
        }

        Integer id = elderlyFamilyRelationService.addRelation(relation);
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "添加关联关系成功");
        response.put("data", id);
        return ResponseEntity.status(HttpStatus.CREATED).body(response);
    }

    /**
     * 更新关联关系
     *
     * @param id       关联ID
     * @param relation 关联关系实体
     * @return 响应结果
     */
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> updateRelation(@PathVariable Integer id,
            @Valid @RequestBody ElderlyFamilyRelation relation) {
        relation.setId(id);
        int rows = elderlyFamilyRelationService.updateRelation(relation);

        Map<String, Object> response = new HashMap<>();
        if (rows > 0) {
            response.put("code", 200);
            response.put("message", "更新关联关系成功");
        } else {
            response.put("code", 404);
            response.put("message", "关联关系不存在");
        }
        return ResponseEntity.ok(response);
    }

    /**
     * 根据ID删除关联关系
     *
     * @param id 关联ID
     * @return 响应结果
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteRelation(@PathVariable Integer id) {
        int rows = elderlyFamilyRelationService.deleteRelation(id);

        Map<String, Object> response = new HashMap<>();
        if (rows > 0) {
            response.put("code", 200);
            response.put("message", "删除关联关系成功");
        } else {
            response.put("code", 404);
            response.put("message", "关联关系不存在");
        }
        return ResponseEntity.ok(response);
    }

    /**
     * 根据老人ID和子女ID删除关联关系
     *
     * @param elderlyId 老人ID
     * @param familyId  子女ID
     * @return 响应结果
     */
    @DeleteMapping("/elderly/{elderlyId}/family/{familyId}")
    public ResponseEntity<Map<String, Object>> deleteRelationByElderlyAndFamily(
            @PathVariable Integer elderlyId,
            @PathVariable Integer familyId) {

        int rows = elderlyFamilyRelationService.deleteRelationByElderlyAndFamily(elderlyId, familyId);

        Map<String, Object> response = new HashMap<>();
        if (rows > 0) {
            response.put("code", 200);
            response.put("message", "删除关联关系成功");
        } else {
            response.put("code", 404);
            response.put("message", "关联关系不存在");
        }
        return ResponseEntity.ok(response);
    }

    /**
     * 根据ID查询关联关系
     *
     * @param id 关联ID
     * @return 响应结果
     */
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getRelationById(@PathVariable Integer id) {
        ElderlyFamilyRelation relation = elderlyFamilyRelationService.getRelationById(id);

        Map<String, Object> response = new HashMap<>();
        if (relation != null) {
            response.put("code", 200);
            response.put("message", "获取关联关系成功");
            response.put("data", relation);
            return ResponseEntity.ok(response);
        } else {
            response.put("code", 404);
            response.put("message", "关联关系不存在");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 根据老人ID查询关联关系列表
     *
     * @param elderlyId 老人ID
     * @return 响应结果
     */
    @GetMapping("/elderly/{elderlyId}")
    public ResponseEntity<Map<String, Object>> getRelationsByElderlyId(@PathVariable Integer elderlyId) {
        List<ElderlyFamilyRelation> relations = elderlyFamilyRelationService.getRelationsByElderlyId(elderlyId);

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "获取老人关联关系列表成功");
        response.put("data", relations);
        return ResponseEntity.ok(response);
    }

    /**
     * 根据子女ID查询关联关系列表
     *
     * @param familyId 子女ID
     * @return 响应结果
     */
    @GetMapping("/family/{familyId}")
    public ResponseEntitys<List<ElderlyFamilyRelation>> getRelationsByFamilyId(@PathVariable Integer familyId) {
        List<ElderlyFamilyRelation> relations = elderlyFamilyRelationService.getRelationsByFamilyId(familyId);
        System.out.println(relations);
        return !relations.isEmpty() ?  ResponseEntitys.success("获取联系人成功",relations,relations.size()):
                ResponseEntitys.success("获取联系人失败",null,relations.size());
    }

    /**
     * 根据老人ID和子女ID查询关联关系
     *
     * @param elderlyId 老人ID
     * @param familyId  子女ID
     * @return 响应结果
     */
    @GetMapping("/elderly/{elderlyId}/family/{familyId}")
    public ResponseEntity<Map<String, Object>> getRelationByElderlyAndFamily(
            @PathVariable Integer elderlyId,
            @PathVariable Integer familyId) {

        ElderlyFamilyRelation relation = elderlyFamilyRelationService.getRelationByElderlyAndFamily(elderlyId,
                familyId);

        Map<String, Object> response = new HashMap<>();
        if (relation != null) {
            response.put("code", 200);
            response.put("message", "获取关联关系成功");
            response.put("data", relation);
            return ResponseEntity.ok(response);
        } else {
            response.put("code", 404);
            response.put("message", "关联关系不存在");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 检查关联关系是否存在
     *
     * @param elderlyId 老人ID
     * @param familyId  子女ID
     * @return 响应结果
     */
    @GetMapping("/check/elderly/{elderlyId}/family/{familyId}")
    public ResponseEntity<Map<String, Object>> checkRelationExists(
            @PathVariable Integer elderlyId,
            @PathVariable Integer familyId) {

        boolean exists = elderlyFamilyRelationService.checkRelationExists(elderlyId, familyId);

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "检查关联关系成功");
        response.put("data", exists);
        return ResponseEntity.ok(response);
    }
}
